﻿package com.wandian.view.mediator
{
	import com.greensock.TweenLite;
	import com.wandian.controller.*;
	import com.wandian.data.GameData;
	import com.wandian.model.*;
	import com.wandian.model.vo.*;
	import com.wandian.util.CLZButton;
	import com.wandian.util.CLZSlide;
	import com.wandian.util.FormatMoney;
	import com.wandian.view.components.ui.AddBtn;
	import com.wandian.view.components.ui.Gift;
	import com.wandian.view.layer.Canvas;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.*;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class SetRoomMediator extends Mediator
	{
		private var totalAssets:uint;
		private var _countDown:Timer;
		private var minChip:int;
		private var maxChip:int;
		private var seatId:int;
		private var _global:GameData;
		private var _roomId:int=0;
		private var _isAgainSetRoom:int=0; //1为快速买入
		private var _inRoomData:InRoomPlayerDataProxy;
		public static const NAME:String="SetRoomMediator";

		private var minBlind:Number;
		private var maxBlind:Number;
		private var minStake:Number;
		private var maxStake:Number;
		private var roomName:String;
		private var _slide:CLZSlide;

		public function SetRoomMediator(viewComponent:Object=null, data:Object=null)
		{
			this._global=GameData.getInstance();
			this._roomId=data.roomid;
			super(NAME, viewComponent);
			this._inRoomData=facade.retrieveProxy(InRoomPlayerDataProxy.NAME) as InRoomPlayerDataProxy;
			return;
		}

		override public function onRegister():void
		{
			this.init();
			return;
		}

		private function init():void
		{
			this.mcSetRoom.btn_ok.addEventListener(MouseEvent.CLICK, this.clickSetRoomHandler);
			AddBtn.initBtn(this.mcSetRoom as MovieClip, CLZNoticeName.SET_ROOM_WINDOS_CLOSE, AddBtn.CLOSE_TWO, "", 2, 2);
			this.mcSetRoom.sub_btn_maxstake.addEventListener(MouseEvent.CLICK, this.clickSubAddHandler);
			this.mcSetRoom.add_btn_maxstake.addEventListener(MouseEvent.CLICK, this.clickSubAddHandler);
			this.mcSetRoom.sub_btn_minstake.addEventListener(MouseEvent.CLICK, this.clickSubAddHandler);
			this.mcSetRoom.add_btn_minstake.addEventListener(MouseEvent.CLICK, this.clickSubAddHandler);
			this.mcSetRoom.sub_btn.addEventListener(MouseEvent.CLICK, this.clickSubAddHandler);
			this.mcSetRoom.add_btn.addEventListener(MouseEvent.CLICK, this.clickSubAddHandler);
			this.mcSetRoom.sub_btn_minblind.addEventListener(MouseEvent.CLICK, this.clickSubAddHandler);
			this.mcSetRoom.add_btn_minblind.addEventListener(MouseEvent.CLICK, this.clickSubAddHandler);
//			this.mcSetRoom.txt_error.addEventListener(TextEvent.LINK, this.linkTxtHandler);
			this.mcSetRoom.title_txt.text="房间设置";
			GameData.stopMcAllChild(this.mcSetRoom.slide_mc);
			this.showSetRoom(this._roomId, GameData._objRoom.minBlind, GameData._objRoom.maxStake, GameData._objRoom.type, this._isAgainSetRoom);
			return;
		}

		private function clickSubAddHandler(event:MouseEvent):void
		{
			var toNum:Number;
			switch (event.target.name)
			{
				//最大携带筹码
				case "sub_btn_maxstake":
					toNum=int(this.mcSetRoom.txt_maxstake.text) - 100;
					if (toNum < 100)
					{
						toNum=100;
					}
					this.mcSetRoom.txt_maxstake.text=toNum;
					break;
				case "add_btn_maxstake":
					this.mcSetRoom.txt_maxstake.text=int(this.mcSetRoom.txt_maxstake.text) + 100;
					break;
				//最小携带筹码
				case "sub_btn_minstake":
					toNum=int(this.mcSetRoom.txt_minstake.text) - 100;
					if (toNum < 100)
					{
						toNum=100;
					}
					this.mcSetRoom.txt_minstake.text=toNum;
					break;
				case "add_btn_minstake":
					this.mcSetRoom.txt_minstake.text=Number(this.mcSetRoom.txt_minstake.text) + 100;
					break;
				//大盲
				case "sub_btn":
					toNum=int(this.mcSetRoom.txt_maxblind.text) - 100;
					if (toNum < 100)
					{
						toNum=100;
					}
					this.mcSetRoom.txt_maxblind.text=toNum;
					break;
				case "add_btn":
					this.mcSetRoom.txt_maxblind.text=Number(this.mcSetRoom.txt_maxblind.text) + 100;
					break;
				//小盲
				case "sub_btn_minblind":
					toNum=int(this.mcSetRoom.txt_minblind.text) - 100;
					if (toNum < 100)
					{
						toNum=100;
					}
					this.mcSetRoom.txt_minblind.text=toNum;
					break;
				case "add_btn_minblind":
					this.mcSetRoom.txt_minblind.text=Number(this.mcSetRoom.txt_minblind.text) + 100;
					break;
				default:
				{
					break;
				}
			}
		}

		private function linkTxtHandler(event:TextEvent):void
		{
//			this.sendNotification(CLZNoticeName.ON_PAY_VIP);
			return;
		}

		private function timerHandler(event:TimerEvent):void
		{
			var _loc_2:*=int(GameData.config["againSetRoomTime"]) - this._countDown.currentCount;
			this.mcSetRoom.countDown_txt.htmlText=GameData.replaceStrValue(GameData.tip["SetRoom_COUNTDOWN"], "TIME", _loc_2);
			if (_loc_2 == 0)
			{
				this.sendNotification(CLZNoticeName.SET_ROOM_WINDOS_CLOSE);
			}
			return;
		}

		private function completeTimerHandler(event:TimerEvent):void
		{
			this.sendNotification(CLZNoticeName.SET_ROOM_WINDOS_CLOSE);
			return;
		}

		private function timerRemove():void
		{
			if (this._countDown != null)
			{
				this._countDown.removeEventListener(TimerEvent.TIMER, this.timerHandler);
				this._countDown.removeEventListener(TimerEvent.TIMER_COMPLETE, this.completeTimerHandler);
				this._countDown=null;
			}
			return;
		}

		public function showSetRoom(_curRoomId:int, _minChip:int, _maxChip:int, roomtype:String, _isAgainSetRoom:int=0):void
		{
			this.mcSetRoom.txt_error.text="";
			this.seatId=_curRoomId;
			this.minChip=_minChip;
			this.maxChip=_maxChip;
			CLZButton.setButton(this.mcSetRoom.btn_ok, "确定设置");
			this._slide=new CLZSlide(this.mcSetRoom.slide_mc.btn, this.mcSetRoom.slide_mc.bg, this.mcSetRoom.slide_mc.sub_btn, this.mcSetRoom.slide_mc.add_btn);
			this._slide.width=this.mcSetRoom.slide_mc.bg.width - this.mcSetRoom.slide_mc.btn.width;
			this._slide.addEventListener(Event.CHANGE, this.changeSlideHandler);
			this._slide.setMinMax(3, 9, 1, 1);
			this.mcSetRoom.txt_maxstake.text=2000;
			this.mcSetRoom.txt_minstake.text=1000;
			this.mcSetRoom.txt_maxblind.text=200;
			this.mcSetRoom.txt_minblind.text=100;
//			this.mcSetRoom.txt_maxstake.text=this._global.userInfo.userVo.money;
//			this.mcSetRoom.txt_minstake.text=int(this._global.userInfo.userVo.money / 50);
//			this.mcSetRoom.txt_maxblind.text=int(this._global.userInfo.userVo.money / 500);
//			this.mcSetRoom.txt_minblind.text=int(this._global.userInfo.userVo.money / 1000);
			return;
		}

		private function changeSlideHandler(event:Event):void
		{
			this.mcSetRoom.txt_roomseat.text=this._slide._value;
			return;
		}

		private function clickSubHandler(event:MouseEvent):void
		{
			var SetRoomMoney:*=int(this.mcSetRoom.txt_SetRoom.text);
			if (SetRoomMoney - this.minChip < this.minChip)
			{
				this.mcSetRoom.txt_SetRoom.text=this.minChip.toString();
				return;
			}
			this.mcSetRoom.txt_SetRoom.text=(SetRoomMoney - this.minChip).toString();
			return;
		}

		private function clickAddHandler(event:MouseEvent):void
		{
			var _loc_2:*=int(this.mcSetRoom.txt_SetRoom.text);
			if (_loc_2 + this.minChip > this.maxChip)
			{
				this.mcSetRoom.txt_SetRoom.text=this.maxChip.toString();
				return;
			}
			this.mcSetRoom.txt_SetRoom.text=(_loc_2 + this.minChip).toString();
			return;
		}

		public function clickSetRoomHandler(event:MouseEvent):void
		{
			roomName=this.mcSetRoom.txt_RoomName.text;
			if (roomName == "")
			{
				this.mcSetRoom.txt_error.htmlText="房间名不能为空";
				return;
			}
			this.minBlind=Number(this.mcSetRoom.txt_minblind.text);
			this.maxBlind=Number(this.mcSetRoom.txt_maxblind.text);
			this.minStake=Number(this.mcSetRoom.txt_minstake.text);
			this.maxStake=Number(this.mcSetRoom.txt_maxstake.text);

			if (minBlind < 1 || minStake < 1 || maxBlind < 1 || maxStake < 1)
			{
				this.mcSetRoom.txt_error.htmlText="设置错误";
				return;
			}
			if (minBlind >= maxBlind)
			{
				this.mcSetRoom.txt_error.htmlText="大小盲注设置错误";
				return;
			}
			if (minStake >= maxStake)
			{
				this.mcSetRoom.txt_error.htmlText="最大最小携带设置错误";
				return;
			}
			if (maxBlind >= minStake)
			{
				this.mcSetRoom.txt_error.htmlText="盲注设置比携带多,设置错误";
				return;
			}
			this.sendNotification(CLZNoticeName.SETROOMINFO, {roomid: this._roomId, roomname: this.roomName, seatnum: int(this._slide._value), minBlind: minBlind, maxBlind: maxBlind, minStake: minStake, maxStake: maxStake});
			this.sendNotification(CLZNoticeName.SET_ROOM_WINDOS_CLOSE);
			return;
		}

		private function clickSetRoomMinHnadler(event:MouseEvent):void
		{
			if (this.totalAssets < this.minChip)
			{
//				if (this._global.judgeMoneyType(GameData._objRoom.type))
//				{
				this.mcSetRoom.txt_error.htmlText=GameData.tip["GAME_GOLD_NOT_ENOUGH"] + "<a href=\"event:pay\">" + GameData.replaceStrTipHtml(GameData.tip["GET_MORE_GLOD"]) + "</a>";
//				}
//				else
//				{
//					this.mcSetRoom.txt_error.htmlText=GameData.tip["GAME_MONEY_NOT_ENOUGH"];
//				}
				return;
			}
			this.SetRoomInfo(this.minChip);
			return;
		}

		private function clickSetRoomMaxHnadler(event:MouseEvent):void
		{
			if (this.totalAssets >= this.minChip)
			{
			}
			if (this.totalAssets < this.maxChip)
			{
				this.SetRoomInfo(this.totalAssets);
				return;
			}
			if (this.totalAssets >= this.maxChip)
			{
				this.SetRoomInfo(this.maxChip);
			}
			return;
		}

		private function SetRoomInfo(money:Number):void
		{
			if (this._isAgainSetRoom)
			{
				this._isAgainSetRoom=0;
//				this.sendNotification(CLZNoticeName.AGAIN_SIT_SetRoom, {seatId: this.seatId, money: param1});
			}
			else
			{
				this.sendNotification(CLZNoticeName.SITDOWN, {seatId: this.seatId, money: money});
			}
			this.sendNotification(CLZNoticeName.SET_ROOM_WINDOS_CLOSE);
			return;
		}

		public function clickCloseHandler():void
		{
			if (this._countDown != null)
			{
				this._countDown.reset();
			}
			this._isAgainSetRoom=0;
			this.timerRemove();
			if (Texas.I._mcWindosView.contains(this.mcSetRoom))
			{
				Texas.I._mcWindosView.removeChildTo(this.mcSetRoom, Canvas.HIDE_MOVE);
				viewComponent=null;
			}
			return;
		}

		public function get nSeatId():int
		{
			return this.seatId;
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.SET_ROOM_WINDOS_CLOSE];
		}

		override public function handleNotification(param1:INotification):void
		{
			switch (param1.getName())
			{
				case CLZNoticeName.SET_ROOM_WINDOS_CLOSE:
				{
					if (this._isAgainSetRoom)
					{
						this.sendNotification(CLZNoticeName.STAND_UP);
					}
					this.clickCloseHandler();
					facade.removeMediator(SetRoomMediator.NAME);
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function get mcSetRoom():MovieClip
		{
			return viewComponent as MovieClip;
		}

	}
}
